package cn.springcloud.fix.oauth.starter;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class MyAccessDeniedHandler implements AccessDeniedHandler {

	@Override
	public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
		log.error(e.getMessage(), e);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("status", -1);
		resultMap.put("message", "权限不足，请联系管理员!");
		PrintWriter out = response.getWriter();
		out.print(JSON.toJSONString(resultMap));
		out.flush();
		out.close();
	}
}
